from medicine import medicine_blue
from database.database import Medicine,db
import json
from flask import jsonify
from utils.jsonutil import AlchemyEncoder
from . import functions



# 获取所有的药品
@medicine_blue.route('/all ',methods=['GET'])
def allmedicine():
    medicines = Medicine.query.all()
    medicine_list = []
    for medicine in medicines:
        medicine_list.append(json.loads(json.dumps(medicine,cls=AlchemyEncoder,ensure_ascii=False)))
    return jsonify({'code':200,'msg':'success','data':medicine_list})


# 通过文字搜索药品
@medicine_blue.route('/search/<text>',methods=['GET'])
def searchMedicine(text):
    medicines = Medicine.query.filter(Medicine.name.like('%'+text+'%')).all()
    medicine_list = []
    for medicine in medicines:
        medicine_list.append(json.loads(json.dumps(medicine,cls=AlchemyEncoder,ensure_ascii=False)))
    return jsonify({'code':200,'msg':'success','data':medicine_list})


# 根据病人id获取病人有效的药品
@medicine_blue.route('/<patient_id>',methods=['GET'])
def getMedicine(patient_id):
    medicine_list = functions.get_valid_medicine(patient_id)
    return jsonify({'code':200,'msg':'success','data':medicine_list})

    